//
//  CZHomeViewController.swift
//  weibo10
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZHomeViewController: CZBaseTableViewController {
    
    
    //MARK: - 属性
    private let reuseIdentifier = "cell"
    
    //要显示的微博数据
    var statuses: [CZStatus]?
        {
    didSet
    {
        //模型数据改变了
        self.tableView.reloadData()
        
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //没有登录就不要执行后面的代码
        if !CZUserAccountViewModel.sharedInstance.isUserLogin
        {
        return
        }
        
        setupNavigationBar()
        
        //注册cell
        tableView.registerClass(CZStatusCell.self,forCellReuseIdentifier:reuseIdentifier)
        
        //设置cell预估行高
        tableView.estimatedRowHeight = 200
        
        //根据cell的约束自动计算行高，测量contentView的高度
        tableView.rowHeight = UITableViewAutomaticDimension
        
        
        //加载微博数据
        CZStatusViewModel.sharedInstance.loadStatus
            { (statuses, error) -> () in
                if error != nil
                {
                CZPrint(items: "请求微博数据失败了: \(error)")
                    
                    return
                }
                
            //请求成功了
                CZPrint(items: "在控制器里面打印获取到的微博数据: \(statuses)")
                
                self.statuses = statuses
        }
       }
    
        
  private  func setupNavigationBar()
  {
    //左边按钮
    navigationItem.leftBarButtonItem = UIBarButtonItem(imageName:"navigationbar_friendsearch",target: self,action: "didClickFriengsearch")
    
    
    //右边按钮
    navigationItem.rightBarButtonItem = UIBarButtonItem(imageName:"navigationbar_pop",target: self,action: "didClickPop")
    
   //获取用户的名称
    let name = CZUserAccountViewModel.sharedInstance.userAccount?.screen_name ?? "未知名称"

    //设置首页titleView
    let button = CZHomeTitleButton()
    
    button.setTitle(name, forState: UIControlState.Normal)
    button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    
    button.titleLabel?.font = UIFont.systemFontOfSize(16)
    button.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
    
    button.addTarget(self, action: "didClickTitleView:", forControlEvents: UIControlEvents.TouchUpInside)
    button.sizeToFit()
    
    navigationItem.titleView = button
    }
    
    //MARK: - titleView的点击事件
    func didClickTitleView(button: UIButton)
    {
    
    //三角形旋转依靠button。selected这个属性来确定
        //1 修改按钮的状态
        button.selected = !button.selected
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            let transform = button.selected ?CGAffineTransformMakeRotation(CGFloat(M_PI - 0.001)) : CGAffineTransformIdentity
            
            button.imageView?.transform = transform
        }
    
    }
    
     func didClickFriengsearch()
     {
    CZPrint(items: "点击了左边按钮")
    
    }

   func didClickPop()
   {
    CZPrint(items: "点击了右边的按钮")
    
    }
}

//扩展CZHomeViewController,tableView显示数据
extension CZHomeViewController
{
//返回cell的数量
    override func tableView(tableView: UITableView,numberOfRowsInSection section: Int) ->Int
    {
    return statuses?.count ?? 0
    }

   //cell显示内容
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //根据identifier去缓存池中查找，缓存池中有就返回一个cell，没有救使用注册的cell类型来创建cell
        let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier,forIndexPath: indexPath) as! CZStatusCell
        //获取到对应的模型
        let status = self.statuses![indexPath.row]
        //1 数据源给cell设置对应的模型
        cell.status = status
        return cell
        
        
    }
    
    
}


